import {
  squared
} from "../../../js/modules/game.js";
import {
  storage,
  util,
  dialog,
  api
} from '../../../js/util';
import {
  router
} from '../../../js/router';
import {
  address
} from '../../../js/modules/store';
import authorize from "../../../js/modules/authorize"
import BasePage from '../../../js/common/base-page'
BasePage({
  pageKey: "gameRecordList",
  /**
   * 页面的初始数据
   */
  data: {
    page: {
      PageIndex: 1,
      PageSize: 20,
      keywords: "",
      sortField: "Id",
      sortDirect: "desc"
    },
    list: [],
    hasMoreData: true,
    forms: {
      ContactMan: {
        value: "",
        validate: "isnull",
        error: "请输入您的姓名"
      },
      ContactTel: {
        value: "",
        validate: "isnull|mobile",
        error: "请输入正确的手机号"
      },
      ContactAddress: {
        value: "",
        validate: "isnull",
        error: "请输入你的联系地址"
      }
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.loadList();
    this.getNearInfo();   //获取默认领奖信息
  },
  /**加载列表 */
  loadList() {
    var that = this;
    var page = this.data.page;
    squared.loadList(page, function (content) {
      var hasMoreData = content.count > (page.PageIndex) * page.PageSize;
      var index = that.data.list.length;
      var data = content.data;
      //当分页大于1时，累加
      if (page.PageIndex > 1) {
        data = that.data.list.concat(data);
      }
      //如果key为变量作为key值的时候要用[]包裹起来
      that.setData({
        list: data,
        hasMoreData: hasMoreData,
        isLoadComplete: true
      });
      // console.log(that.data.list);
    })
  },
  /**
   * 获取默认中奖者信息
   */
  getNearInfo(){
    let that = this;
    let {forms} = that.data;
    squared.getNearInfo((res)=>{
      let {ContactAddress,ContactMan,ContactTel,AwardTime} = res;
      if(AwardTime==''){
        address.getDefaultAddress(function(ads){
          if (ads!=undefined&&Object.keys(ads).length>0) {
            forms.ContactMan.value = ads.Name;
            forms.ContactTel.value = ads.Mobile;
            forms.ContactAddress.value = ads.Province + ads.City + ads.Area + ads.Address;
            that.setData({ forms: forms });
          }
        })
      }else{
        forms.ContactMan.value = ContactMan;
        forms.ContactTel.value = ContactTel;
        forms.ContactAddress.value = ContactAddress;
        that.setData({
          forms:forms
        })
      }
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    this.data.page.PageIndex = 1;
    this.loadList();
    // console.log("onPullDownRefresh");
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function (e) {
    // console.log("onReachBottom");
    if (this.data.hasMoreData) {
      var index = this.data.page.PageIndex;
      // console.log(index + 1);
      this.data.page.PageIndex = index + 1;
      this.loadList();
    }
  },
  /**
   * 领取奖品
   */
  onAwardEvent: function (e) {
    var idx = e.currentTarget.dataset.idx;
    this.setData({
      idx: idx,
      showShadowInfo: true
    });
  },
  /**
   * 领取奖品事件
   */
  onReceivedAwardEvent: function (e) {
    var that = this;
    var current=this.data.list[this.data.idx];
    squared.submitReceivedAward(this.data.forms, { ResultId: current.Id}, function () {
      current.AwardStatus=10;

      that.setData({
        list:that.data.list
      })
      that.closeShadow();
      util.success("提交成功");
    });
  },
  /**
   * 表单值绑定
   */
  bindFormValue: function (e) {
    this.data.forms[e.target.id].value = e.detail.value;

  },
  /**
 * 关闭弹层
 */
  closeShadow: function () {
    this.setData({
      showShadowInfo: false
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})